package com.letoken.platform.admin.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.pub.util.HttpClientUtil;
import com.letoken.platform.repo.ecommerce.dto.KuaidiReq;
import com.letoken.platform.repo.ecommerce.vo.KuaidiInfoRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CainiaoUtils {

    private static final String key="a93c3f91b1894e41e8c5ec71286b2de1";
    private static final String url="http://api.cainiaoapi.com/api/exp/v1/index";


    private static StringRedisTemplate stringRedisTemplate;


    public static void setRedisTemplate(StringRedisTemplate redisTemplate) {
        if (ObjectUtils.isEmpty(stringRedisTemplate)){
            stringRedisTemplate = redisTemplate;
        }

    }

    public static KuaidiInfoRes query(KuaidiReq req){
        String st = "";
        try {
            String  info = stringRedisTemplate.opsForValue().get(req.getNo());
            if (org.apache.commons.lang3.StringUtils.isEmpty(info) || req.getIsRealTime()){
                Map<String, String> map = new HashMap<>();
                map.put("key",key);
                map.put("com","auto");
                map.put("no",req.getNo().trim());
                if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(req.getPhone(),"sf")) map.put("phone",req.getPhone());
                st = JSONObject.parseObject(HttpClientUtil.sendGet(url,map,null)).get("code").toString();
                if(org.apache.commons.lang3.StringUtils.equals(st,"1")){
                    info = JSONObject.parseObject(HttpClientUtil.sendGet(url,map,null)).get("data").toString();
                    stringRedisTemplate.opsForValue().set(req.getNo(),info,30, TimeUnit.MINUTES);
                }
            }
            return JSONObject.parseObject(info, KuaidiInfoRes.class);
        } catch (Exception e) {
            log.error("查询物流信息失败! {} {}", JSON.toJSONString(req),st,e);
        }
        return null;
    }


    public static void main(String[] args) {
        KuaidiReq req = new KuaidiReq();
        req.setPhone("1149");
        req.setNo("SF1616740902855");


        String st = "";
        try {
            String  info = null;
            if (org.apache.commons.lang3.StringUtils.isEmpty(info) || req.getIsRealTime()){
                Map<String, String> map = new HashMap<>();
                map.put("key",key);
                map.put("com","auto");
                map.put("no",req.getNo());
                if(org.apache.commons.lang3.StringUtils.containsIgnoreCase(req.getPhone(),"sf")) map.put("phone",req.getPhone());
                st = JSONObject.parseObject(HttpClientUtil.sendGet(url,map,null)).get("code").toString();
                if(org.apache.commons.lang3.StringUtils.equals(st,"1")){
                    info = JSONObject.parseObject(HttpClientUtil.sendGet(url,map,null)).get("data").toString();
                    stringRedisTemplate.opsForValue().set(req.getNo(),info,30, TimeUnit.MINUTES);
                }
            }
            System.out.println(JSONObject.parseObject(info, KuaidiInfoRes.class));
        } catch (Exception e) {
            log.error("查询物流信息失败! {} {}", JSON.toJSONString(req),st,e);
        }
    }

}
